package com.woniu.controller.sportsHealth;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;

import com.woniu.model.sportsHealth.model.dto.BloodSugarDto;
import com.woniu.model.sportsHealth.model.vo.BloodSugarVo;
import com.woniu.service.sportsHealth.IBloodSugarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 血糖表 前端控制器
 * </p>
 *
 * @author Jet
 * @since 2023-10-23
 */
@RestController
@RequestMapping("/blood-sugar")
public class BloodSugarController {

    @Autowired
    IBloodSugarService iBloodSugarService;
    @PostMapping("/findAll")
    public ResponseEntity<PageInfo<BloodSugarDto>> findAll(@RequestBody Map<String,Object> param){
        Integer pageNum = (Integer) param.get("pageNum");
        Integer pageSize = (Integer) param.get("pageSize");
        PageInfo<BloodSugarDto> pageInfo = iBloodSugarService.findAll(param, pageNum, pageSize);
        return ResponseEntity.ok(pageInfo);
    }

    @GetMapping("/export")
    public void sugarPort(HttpServletResponse response)throws IOException {
        List<BloodSugarVo> list = iBloodSugarService.findAllExport();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("content-disposition","attachment;filename=parking.xlsx");

        ServletOutputStream stream = response.getOutputStream();
        EasyExcel.write(stream, BloodSugarVo.class)
                .sheet("血糖表")
                .doWrite(list);

    }
}
